/**
*      author: Y_Yao
*      created: 2024-10-19 15:29:43
**/

#include<bits/stdc++.h>

using namespace std;

#define lc p<<1
#define rc p<<1|1
#define int LL
using LL = long long;
const LL LLINF = 1e18;
const int INF = 0x3f3f3f3f;
const int N = 1e5 + 7;
const int mod = 10007;
int n,m;

struct node
{
    int l,r,sum1,sum2,sum3,ass,mul,add;
}tr[4 * N];

void pushup(int p)
{
    tr[p].sum1 = (tr[lc].sum1 + tr[rc].sum1) % mod;
    tr[p].sum2 = (tr[lc].sum2 + tr[rc].sum2) % mod;
    tr[p].sum3 = (tr[lc].sum3 + tr[rc].sum3) % mod;
}

void pushdown(int p)
{
    if(tr[p].ass)
    {
        tr[lc].sum1 = tr[p].ass * (tr[lc].r - tr[lc].l + 1) % mod;
        tr[rc].sum1 = tr[p].ass * (tr[rc].r - tr[rc].l + 1) % mod;

        tr[lc].sum2 = tr[p].ass * tr[p].ass%mod * (tr[lc].r - tr[lc].l + 1) % mod;
        tr[rc].sum2 = tr[p].ass * tr[p].ass%mod * (tr[rc].r - tr[rc].l + 1) % mod;

        tr[lc].sum3 = tr[p].ass * tr[p].ass%mod * tr[p].ass%mod * (tr[lc].r - tr[lc].l + 1) % mod;
        tr[rc].sum3 = tr[p].ass * tr[p].ass%mod * tr[p].ass%mod * (tr[rc].r - tr[rc].l + 1) % mod;


        tr[lc].ass = tr[p].ass;
        tr[lc].add = 0;
        tr[lc].mul = 1;

        tr[rc].ass = tr[p].ass;
        tr[rc].add = 0;
        tr[rc].mul = 1;

        tr[p].ass = 0;
    }
    if(tr[p].mul != 1)
    {
        int c = tr[p].mul;
        tr[lc].sum1 = c * tr[lc].sum1 % mod;
        tr[rc].sum1 = c * tr[rc].sum1 % mod;

        tr[lc].sum2 = c * c%mod * tr[lc].sum2 % mod;
        tr[rc].sum2 = c * c%mod * tr[rc].sum2 % mod;

        tr[lc].sum3 = c * c%mod * c%mod * tr[lc].sum3 % mod;
        tr[rc].sum3 = c * c%mod * c%mod * tr[rc].sum3 % mod;

        tr[lc].mul = c * tr[lc].mul % mod;
        tr[rc].mul = c * tr[rc].mul % mod;

        tr[lc].add = c * tr[lc].add % mod; 
        tr[rc].add = c * tr[rc].add % mod; 

        tr[p].mul = 1;
    }
    if(tr[p].add)
    {
        int c = tr[p].add;

        int s1 = tr[lc].sum1;
        int s2 = tr[lc].sum2;

        int len1 = tr[lc].r - tr[lc].l + 1;
        tr[lc].sum1 = (s1 + len1 * c) % mod;
        tr[lc].sum2 = (s2 + 2 * s1%mod * c%mod + len1 * c%mod * c%mod) % mod;
        tr[lc].sum3 = (tr[lc].sum3 + 3 * c * s2%mod + 3 * s1 * c%mod * c%mod + len1 * c%mod * c%mod * c%mod) % mod;

        s1 = tr[rc].sum1;
        s2 = tr[rc].sum2;

        len1 = tr[rc].r - tr[rc].l + 1;
        tr[rc].sum1 = (s1 + len1 * c) % mod;
        tr[rc].sum2 = (s2 + 2 * s1%mod * c%mod + len1 * c%mod * c%mod) % mod;
        tr[rc].sum3 = (tr[rc].sum3 + 3 * c * s2%mod + 3 * s1 * c%mod * c%mod + len1 * c%mod * c%mod * c%mod) % mod;
        
        tr[lc].add = (tr[lc].add + c) % mod;
        tr[rc].add = (tr[rc].add + c) % mod;

        tr[p].add = 0;
    }
}

void build(int p,int l,int r)
{
    tr[p] = {l,r,0,0,0,0,1,0};
    if(l == r) return;

    int mid = (l + r) >> 1;
    build(lc,l,mid);
    build(rc,mid + 1,r);
    pushup(p);
}

void update1(int p,int x,int y,int c)
{
    if(x <= tr[p].l && tr[p].r <= y)
    {
        int s1 = tr[p].sum1;
        int s2 = tr[p].sum2;
        tr[p].sum1 = (tr[p].sum1 + c * (tr[p].r - tr[p].l + 1)) % mod;
        tr[p].sum2 = (tr[p].sum2 + 2 * s1%mod * c%mod + c * c%mod * (tr[p].r - tr[p].l + 1)%mod)%mod;
        tr[p].sum3 = (tr[p].sum3 + 3*c*s2%mod+3*s1*c*c%mod+(tr[p].r - tr[p].l + 1)*c%mod*c%mod*c%mod)%mod;
        tr[p].add = (c + tr[p].add)%mod;
        return;
    }

    int mid = (tr[p].l + tr[p].r) >> 1;
    pushdown(p);
    if(x <= mid) update1(lc,x,y,c);
    if(y > mid) update1(rc,x,y,c);
    pushup(p);
}

void update2(int p,int x,int y,int c)
{
    if(x <= tr[p].l && tr[p].r <= y)
    {
        tr[p].sum1 = tr[p].sum1 * c % mod;
        tr[p].sum2 = tr[p].sum2 * c%mod * c%mod;
        tr[p].sum3 = tr[p].sum3 * c%mod * c%mod * c % mod;
        tr[p].add = (c * tr[p].add)%mod;
        tr[p].mul = (c * tr[p].mul)%mod;
        return;
    }

    int mid = (tr[p].l + tr[p].r) >> 1;
    pushdown(p);
    if(x <= mid) update2(lc,x,y,c);
    if(y > mid) update2(rc,x,y,c);
    pushup(p);
}

void update3(int p,int x,int y,int c)
{
    if(x <= tr[p].l && tr[p].r <= y)
    {
        tr[p].sum1 = (tr[p].r - tr[p].l + 1) * c % mod;
        tr[p].sum2 = (tr[p].r - tr[p].l + 1) * c% mod * c % mod;
        tr[p].sum3 = (tr[p].r - tr[p].l + 1) * c % mod * c % mod * c % mod;
        tr[p].ass = c;
        tr[p].mul = 1;
        tr[p].add = 0;
        return;
    }

    int mid = (tr[p].l + tr[p].r) >> 1;
    pushdown(p);
    if(x <= mid) update3(lc,x,y,c);
    if(y > mid) update3(rc,x,y,c);
    pushup(p);
}

int qurey(int p,int x,int y,int c)
{
    if(x <= tr[p].l && tr[p].r <= y)
    {
        if(c == 1) return tr[p].sum1;
        else if(c == 2) return tr[p].sum2;
        else return tr[p].sum3;
    }

    int mid = (tr[p].l + tr[p].r) >> 1;
    pushdown(p);
    int sum = 0;
    if(x <= mid) sum = (sum + qurey(lc,x,y,c)) % mod;
    if(y > mid) sum = (sum + qurey(rc,x,y,c)) % mod;
    return sum;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    while(cin >> n >> m)
    {
        if(n == 0 && m == 0) break;
        memset(tr,0,sizeof(tr));
        build(1,1,n);
        while(m--)
        {
            int oper,x,y,c;
            cin >> oper >> x >> y >> c;
            if(oper == 1)
            {
                update1(1,x,y,c);
            }
            else if(oper == 2)
            {
                update2(1,x,y,c);
            }
            else if(oper == 3)
            {
                update3(1,x,y,c);
            }
            else
            {
                int out = qurey(1,x,y,c);
                cout << out << endl;
            }
        }
    }
    return 0;
}